<?php

namespace addons\booking\model;

use think\Db;
use think\Model;


class StoreUser extends Model
{

    // 表名
    protected $name = 'store_user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    /**
     * 门店列表
     *
     * @param array $param
     * @return \think\Paginator
     */
    public static function tableList($param)
    {
        if(!isset($param['store_id'])) return [];
        $pageNum = 15;
        if (isset($param['num']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }
        $where = [];
        $where['a.deletetime'] = ['=',0];
        if(isset($param['store_id']) && $param['store_id'] > 0){
            $where['a.store_id'] = ['=',$param['store_id']];
        }

        $list = self::field('a.*,u.logintime')
            ->alias('a')
            ->join('user u','a.uid = u.id')
            ->where($where)
            ->order('weigh desc,id desc')
            ->paginate($pageNum);

        foreach ($list as $item) {
            $order_count = 0;
            $item['order_count'] = 0;
        }

        return $list;
    }

    //设置值班状态
    public static function set_work_status($store_id = 0,$uid = 0)
    {
        #将酒店所有人设置成为不值班
        self::where('store_id',$store_id)->update(['work_status'=>0]);

        #将当前用户设置为值班状态
        self::where('store_id',$store_id)->where('uid',$uid)->update(['work_status'=>1]);

        return true;
    }
}
